asm
  ld sp,$CFFF
  end asm

#include "./library/smsrnd.bas"
#include "./library/pacmandelay.bas"
#include "./library/pacmanfillram.bas"
#include "./library/pacmanldir.bas"


'-------------------------------------------------------------------------------
'----- variables

dim ee1x as uinteger at $C050
dim ee1y as uinteger at $C052
dim ee1q as uinteger at $C054
dim eelp as uinteger at $C056
dim rompos as uinteger at $C058
dim snkln as uinteger at $C05A
dim snklndc as uinteger at $C05C
dim eesd as uinteger at $C05E
dim xntz as uinteger at $C060
dim yntz as uinteger at $C062
dim keybbf as ubyte at $C064
dim gameover as ubyte at $C065
dim xdir as byte at $C066
dim ydir as byte at $C067
dim xpos as byte at $C068
dim ypos as byte at $C069
dim ccur as ubyte at $C06A
dim colisn as ubyte at $C06B
dim keyband as ubyte at $C06C


'----- variables to be removed
dim i as uinteger at $C010
dim eee as uinteger at $C012
dim seed as uinteger at $C014
dim ee1 as uinteger at $C022
dim ee2 as uinteger at $C016
dim ee3 as uinteger at $C018
dim ee4 as ubyte at $C01A
dim ecsp as ubyte at $C01C
dim ecsq as ubyte at $C01E
dim ej0 as uinteger at $C020
dim ex2 as uinteger at $C028
dim ey2 as uinteger at $C02A
dim ex3 as uinteger at $C02C
dim ey3 as uinteger at $C02E
dim ex0 as uinteger at $C030
dim ey0 as uinteger at $C032
dim ex1 as uinteger at $C034
dim ey1 as uinteger at $C036
dim ex6 as uinteger at $C038
dim ey6 as uinteger at $C03A
dim ex7 as uinteger at $C03C
dim ey7 as uinteger at $C03E
dim ex4 as uinteger at $C040
dim ey4 as uinteger at $C042
dim ex5 as uinteger at $C044
dim ey5 as uinteger at $C046
dim zzz as uinteger at $C048

'-------------------------------------------------------------------------------
'----- routines

sub pbactionputsprite(tlayer as ubyte,tx as ubyte,ty as ubyte,tid as ubyte,tclut as ubyte):
  poke $E000+((tlayer band 31)*4),tid          '- shape sprite 0
  poke $E001+((tlayer band 31)*4),tclut        '- clut? sprite 0
  'poke $E002+((tlayer band 31)*4),tx  bxor $ff '- x sprite 0
  'poke $E003+((tlayer band 31)*4),ty bxor $ff  '- y sprite 0
  poke $E002+((tlayer band 31)*4),tx   '- x sprite 0
  poke $E003+((tlayer band 31)*4),ty   '- y sprite 0
  end sub

sub pbactionpalette(tid1 as uinteger,tvl1 as uinteger):
  '$E400:gr ; $E401:xb
  poke $E400+(tid1*2),((tvl1 band $0F00)/256) bor (tvl1 band $F0)
  poke $E401+(tid1*2),tvl1 band $FF
  end sub

sub pbactionpalettepeekuinteger(tid1 as uinteger,tvl1 as uinteger):
  '$E400:gr ; $E401:xb
  poke $E401+(tid1*2),((tvl1 band $0F00)/256) bor (tvl1 band $F0)
  poke $E400+(tid1*2),tvl1 band $FF
  end sub

sub pbactionputchar(txp1 as uinteger,typ1 as uinteger,tch1 as ubyte,tat1 as ubyte, tla1 as uinteger):
  poke ($D000+(tla1*$800)+(txp1*32)+typ1) bxor %0000001111100000,tch1
  poke ($D400+(tla1*$800)+(txp1*32)+typ1) bxor %0000001111100000,tat1
  end sub

sub pbactionwritetext(txp2 as uinteger,typ2 as uinteger,txloc2 as uinteger,tlen2 as uinteger,tat2 as ubyte, tla2 as uinteger):
  dim twrt2 as uinteger at $C000
  for twrt2=0 to tlen2-1
    pbactionputchar(txp2+twrt2,typ2,peek(txloc2+twrt2),tat2,tla2)
    next
  end sub


function peekmap(txp1 as uinteger,typ1 as uinteger) as ubyte
  return peek(($D800+(txp1*32)+typ1) bxor %0000001111100000)
  end function


'-------------------------------------------------------------------------------
'----- init palette

'seed=0
'for eee=0 to 255
'  pbactionpalettepeekuinteger(eee,peek(uinteger,@palettetest01z1+(eee*2)) )
'  next
'for eee=1 to 10:  seed=smsrnd(seed):next


'for eee=0 to 255
'  seed=smsrnd(seed)
'  pbactionpalettepeekuinteger(eee,seed)
'  next

'pbactionpalette(1,$A96)
'pbactionpalette(128,$156)
'
'pbactionpalette(1,$F55)
'pbactionpalette(2,$5F5)
'pbactionpalette(3,$55F)

for eee=0 to 15
  'pbactionpalettepeekuinteger(eee,peek(uinteger,@palette02+(eee*2)))
  'pbactionpalettepeekuinteger(eee+128,peek(uinteger,@palette02+(eee*2)))
  pbactionpalette(eee,peek(uinteger,@palette02+(eee*2)))
  pbactionpalette(eee+128,peek(uinteger,@palette02+(eee*2)))

  next


'-------------------------------------------------------------------------------
'-------------------------------------------------------------------------------
'----- main game loop
loopmain:

'----- cls
pacmanfillram($D800,96,1024)
pacmanfillram($DC00,0,1024)


'----- writing title
pbactionwritetext(3,25,@text01,@text01end-@text01,0,1)
pbactionwritetext(3,26,@text02,@text02end-@text02,0,1)
pbactionwritetext(3,28,@text03,@text03end-@text03,0,1)

'do:loop

'-------------------------------------------------------------------------------
'----- keyboard loop
looptitlekbchk:

keyband=peek($E600) 'band %00011101
if keyband=0 then:
  goto looptitlekbchk
  end if

gameover=0

'-------------------------------------------------------------------------------
'----- cls
pacmanfillram($D800,96,1024)
pacmanfillram($DC00,0,1024)

'-------------------------------------------------------------------------------
'- variables
colisn=0:snklndc=12
ccur=0:colisn=0
snkln=8:snklndc=12:rompos=0
xpos=12:ypos=14
xdir=1:ydir=0

'-------------------------------------------------------------------------------
'- random
eesd=smsrnd(eesd):xntz=(eesd mod 26)+1
eesd=smsrnd(eesd):yntz=(eesd mod 30)+1

'-------------------------------------------------------------------------------

'-------------------------------------------------------------------------------
'- clean map?
'for ee1x=$C000 to $E000
'  poke ee1x,0
'  next
'for ee1x=$C000 to $C000+3391
'  poke ee1x,code("A")
'  'poke ee1x,ee1x
'  next



'-------------------------------------------------------------------------------
'----- game loop
loopgame:

keybbf=peek($E600)
if (keybbf band 1)<>0 and (ydir<>0) then:xdir= 1:ydir= 0:end if
if (keybbf band 4)<>0 and (ydir<>0) then:xdir=-1:ydir= 0:end if
if (keybbf band 8)<>0 and (xdir<>0) then:xdir= 0:ydir= 1:end if
if (keybbf band 16)<>0 and (xdir<>0) then:xdir= 0:ydir=-1:end if

eesd=smsrnd(eesd bxor keybbf)
xpos=xpos+xdir
ypos=ypos+ydir

if (xpos=xntz) and (ypos=yntz) then:
  eesd=smsrnd(eesd):xntz=(eesd mod 26)+1
  eesd=smsrnd(eesd):yntz=(eesd mod 30)+1
  snkln=snkln+1
  end if

if ypos<0 then:ypos=31:end if
if xpos<0 then:xpos=27:end if
if ypos>31 then:ypos=0:end if
if xpos>27 then:xpos=0:end if


'----- movimento da cobra
'for eelp=$CEFD-(snkln*2) to $CEFD step 2
for eelp=$CEFD-1024 to $CEFD step 2
  poke uinteger eelp,peek(uinteger,eelp+2)
  next
poke $CEFE,xpos:poke $CEFF,ypos


'- put8x8charlaced(4,2,peekmap(xpos,ypos,$C000),@bin01)  '----- debug

'----- draw head and tail of the snake, and the pokes are not working?
ccur=(ccur+1)band 15

'pokemap(peek($EFFE-(snkln*2)),peek($EFFF-(snkln*2)),code("B"),$C000)
'put8x8charlaced(peek($EFFE-(snkln*2)),peek($EFFF-(snkln*2)),$20,@bin01)
pbactionputchar(peek($CEFE-(snkln*2))+2,peek($CEFF-(snkln*2)),$20,0,1)

'- replace with random, and check collision
'put8x8charlaced(xntz,yntz,ccur,@bin01)
pbactionputchar(xntz+2,yntz,32,0,1)
if peekmap(xpos+2,ypos)<$20 then:
  gameover=1
  end if
pbactionputchar(xntz+2,yntz,ccur+97,0,1)

'pokemap(xpos,ypos,code("C"),$C000)
'put8x8charlaced(xpos,ypos,ccur,@bin01)
pbactionputchar(xpos+2,ypos,ccur+16,0,1)

pacmandelay(2000)

  'asm
  '  halt
  '  end asm

  if gameover=0 then
    goto loopgame
    end if

for ee1x=1 to 50
  pacmandelay(2000)
  'asm
  '  halt
  '  end asm
  next

  goto loopmain

'-------------------------------------------------------------------------------

do:loop

'-------------------------------------------------------------------------------

palette02:
asm
  defw $BBB,$422,$B96,$B99
  defw $200,$442,$444,$644
  defw $942,$944,$664,$966
  defw $B64,$DB9,$555,$AAA
  end asm

'-------------------------------------------------------------------------------

text01:
asm
  defb " JUST ANOTHER SNAKE GAME "
  end asm
text01end:
text02:
asm
  defb " PAULO SILVA, '13-'15 "
  end asm
text02end:
text03:
asm
  defb " PUSH ANY KEY "
  end asm
text03end:

'-------------------------------------------------------------------------------


